from gevent import monkey, spawn;

monkey.patch_all()

import socket
import hashlib


def con(conn, addr):
    while True:
        try:
            get_data = conn.recv(1024)
        except:
            break
        get_data = get_data.decode('utf-8')
        if get_data == 'q':
            break
        has = hashlib.sha256()
        has.update(get_data.encode("utf-8"))
        put_data = has.hexdigest()
        get_data += ":"
        conn.send(get_data.encode("utf-8") + put_data.encode("utf-8"))
    print(f"{addr}退出连接！")
    conn.close()


def run(ip, port):
    sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sk.bind((ip, port))
    sk.listen(5)
    while True:
        conn, addr = sk.accept()
        print(f"{addr}连接成功！")
        spawn(con, conn, addr)


if __name__ == '__main__':
    g = spawn(run("127.0.0.1", 5000))
    g.join()
